package chat.chit.tit.mit;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Presence;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import chat.chit.tit.mit.adapter.ContactsAdapter;
import chat.chit.tit.mit.model.PersonEntity;
import chat.chit.tit.mit.variables.Variables;

public class ContactsFragment extends Fragment {

	private ListView lvContacts;
	private ContactsAdapter contactsAdapter;
	private List<PersonEntity> listPerson;
	private ImageView btnSearchFriend;

	public ContactsFragment() {
		listPerson = new ArrayList<PersonEntity>();
		initData();

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_contacts, container,
				false);
		btnSearchFriend = (ImageView) rootView.findViewById(R.id.btn_search);
		lvContacts = (ListView) rootView.findViewById(R.id.lvContacts);
		contactsAdapter = new ContactsAdapter(getActivity(), listPerson);
		lvContacts.setAdapter(contactsAdapter);
		lvContacts.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long id) {
				gotoChatContent(position);
			}
		});
		
		btnSearchFriend.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ContactSearchFragment search = new ContactSearchFragment();
				Bundle data = new Bundle();
				search.setArguments(data);

				((MainActivity) getActivity()).replaceCurrentFragment(search);
				((MainActivity) getActivity()).setTitle("Search Friend");
			}
		});
		return rootView;
	}

	public void initData() {
		XMPPConnection conn = Variables.connection;
		if (conn != null) {
			Roster roster = conn.getRoster();
			Collection<RosterEntry> entries = roster.getEntries();
			PersonEntity person = null;
			for (RosterEntry entry : entries) {
				person = new PersonEntity();
				person.setEmail(entry.getStatus() == null ? "" : entry
						.getStatus() + "");
				person.setFullName(entry.getUser());

				Presence entryPresence = roster.getPresence(entry.getUser());
				Presence.Type type = entryPresence.getType();
				if (type == Presence.Type.available) {
					person.setStatus(true);
				} else {
					person.setStatus(false);
				}
				listPerson.add(person);
			}
		}
	}

	private void gotoChatContent(int position) {
		ChatContentFragment Chat = new ChatContentFragment(listPerson.get(position).getFullName());
		Bundle data = new Bundle();
		Chat.setArguments(data);

		((MainActivity) getActivity()).replaceCurrentFragment(Chat);
		((MainActivity) getActivity()).setTitle(listPerson.get(position).getFullName());

	}
	
}
